define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'notice/index',
                    add_url: 'notice/add',
                    edit_url: 'notice/edit',
					del_url: 'notice/del',
                    table: 'notice',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'releasetime',
				sortOrder: 'desc',
                commonSearch: false,
                search: false,
				showColumns: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'sender', title: __('发送人'), width:'8%', formatter:function (value, row, index) {
                                var html = '<img src="'+row.sender.avatar+'" width="40" /> '+row.sender.nickname;
								return html;
                        }},
						{field: 'departs', title: __('接收单位'), width:'10%', formatter:function (value, row, index) {
                                if(value)
								{
									return value;
								}
								else
								{
									return '全部机构';
								}
                        }, visible:Config.hasAuth?true:false},
						{field: 'receivers', title: __('接收人'), width:'10%', formatter:function (value, row, index) {
                                if(value)
								{
									return value;
								}
								else
								{
									return '全部人员';
								}
                        }, visible:Config.hasAuth?true:false},
                        {field: 'content', title: __('内容'), width:'60%', align:'left', formatter:function (value, row, index) {
                                var html = value+'<br>'+row.releasetime;
								return html;
                        }},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons:[
								{
									name: 'edit',
									title: function(row){
										if(row.readed == 1)
										{
											return '查看';
										}
										else
										{
											return '未读';
										}
									},
									text:function(row){
										if(row.readed == 1)
										{
											return '查看';
										}
										else
										{
											return '未读';
										}
									},
									classname: 'btn btn-xs btn-primary btn-dialog',
									url: $.fn.bootstrapTable.defaults.extend.edit_url,
									extend:function(row){
										var html = 'data-area=\'["40%","40%"]\'';
										if(row.readed == 0)
										{
											html += 'style="background-color: #f39c12;"';
										}
										return html;
									}
								}
							], formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
			Controller.api.bindevent();
			
			$("#receiver_ids").data("params", function (obj) {
				return {depart_ids: $('select[name="row[depart_ids][]"]').val()};
			});
			
			$(document).on("change", 'select[name="row[depart_ids][]"]', function () {
				$('#receiver_ids').selectPageClear();
			});
			
			$(document).on('click','button[type="reset"]', function(){
				var index = parent.Layer.getFrameIndex(window.name);
				parent.Layer.close(index);
			});
        },
        edit: function () {
			$(document).on('click','button[type="reset"]', function(){
				var index = parent.Layer.getFrameIndex(window.name);
				parent.Layer.close(index);
				
				parent.$("#table").bootstrapTable('refresh', {});
			});
				
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
